﻿$(function() {
	
	$('#choose').bind('click',function(){
		
		if( $.trim($('#friendName').val()) != '' && $.trim($('#friendUrl').val()) != '' ){
			var answer = $('#answer').val();
			var rightAnswer = MECHOICE.GLOBLE.ANSWER[$('#answer').data("index")];
			if( answer == rightAnswer ){
				$.ajax({
				 type: "POST",
				 url: contextPath + "/saveFriend.action" ,
				 data : $('#artForm').serializeArray(),
				 success: function(msg){
					if( msg ){
						$('#msg').text(msg);
						$('#friendName').val("");
						$('#friendUrl').val("");
						MECHOICE.Method.refreshRandomQuestion();
					}
				 }
			});
			}else{
				$('#answer').val("答案不正确呀，给力。");
			}
			
		}else{
			$('#msg').text("东西没填齐呢，朋友。");
		}
		
		/*	
		if( pass ){
			$('#artForm').submit();
		}else{
			return;
		}
		*/

	});
	
	$.ajax({
		 type: "POST",
		 url: contextPath + "/reveicePass.action" ,
		 success: function(msg){
			if( msg ){
				var msgArr = msg.split('`');
				MECHOICE.GLOBLE.QUESTION = eval(msgArr[0]);
				MECHOICE.GLOBLE.ANSWER = eval(msgArr[1]);
				MECHOICE.Method.refreshRandomQuestion();
			}
		 }
	});
	
});

MECHOICE.Method.refreshRandomQuestion = function(){
	var randomNum = Math.round(Math.random()*9);
	$('#question').text(MECHOICE.GLOBLE.QUESTION[randomNum]);
	$('#answer').data("index",randomNum);
}